package com.xhs.service.impl;

import com.xhs.dto.request.AddEventParams;
import com.xhs.dto.request.AddLowCodeUrlParams;
import com.xhs.dto.request.FindLowCodeUrlParams;
import com.xhs.entity.mongo.Event;
import com.xhs.entity.mongo.LowCodeUrl;
import com.xhs.entity.mongo.User;
import com.xhs.message.Result;
import com.xhs.message.ReturnResult;
import com.xhs.service.MongoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @desc:
 * @projectName: spring-boot-demo
 * @author: xhs
 * @date: 2023-11-23 023 16:20
 * @version: JDK 1.8
 */
@Slf4j
@Service
public class MongoServiceImpl implements MongoService {

    @Resource
    @Qualifier("mongoTemplate")
    private MongoTemplate mongoTemplate;

    @Resource
    @Qualifier("mongoTemplateLowCode")
    private MongoTemplate mongoTemplateLowCode;

    @Resource
    @Qualifier("mongoTemplateEvent")
    private MongoTemplate mongoTemplateEvent;


    /**
     * 查询所有用户
     *
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> getUser() {
        List<User> users = mongoTemplate.find(new Query(), User.class);
        return ReturnResult.build(Result.QUERY_SUCCESS).setData(users);
    }

    /**
     * 条件查询 LowCodeUrl
     *
     * @param findLowCodeUrlParams 查询条件
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> getLowCodeUrl(FindLowCodeUrlParams findLowCodeUrlParams) {
        Query query = new Query();
        if (StringUtils.hasLength(findLowCodeUrlParams.getSystemName())) {
            query.addCriteria(Criteria.where("systemName").regex(findLowCodeUrlParams.getSystemName()));
        }
        if (StringUtils.hasLength(findLowCodeUrlParams.getObjectName())) {
            query.addCriteria(Criteria.where("objectName").regex(findLowCodeUrlParams.getObjectName()));
        }
        if (StringUtils.hasLength(findLowCodeUrlParams.getInterfaceType())) {
            query.addCriteria(Criteria.where("interfaceType").is(findLowCodeUrlParams.getInterfaceType()));
        }
        List<LowCodeUrl> lowCodeUrlList = mongoTemplateLowCode.find(query, LowCodeUrl.class);
        return ReturnResult.build(Result.QUERY_SUCCESS).setData(lowCodeUrlList);
    }

    /**
     * 新增 LowCodeUrl 数据
     *
     * @param lowCodeUrlParams LowCodeUrl 数据
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> addLowCodeUrl(AddLowCodeUrlParams lowCodeUrlParams) {
        Date date = new Date();
        LowCodeUrl codeUrl = new LowCodeUrl();
        codeUrl.setSystemName(lowCodeUrlParams.getSystemName());
        codeUrl.setObjectName(lowCodeUrlParams.getObjectName());
        codeUrl.setInterfaceUrl(lowCodeUrlParams.getInterfaceUrl());
        codeUrl.setInterfaceType(lowCodeUrlParams.getInterfaceType());
        codeUrl.setCreateTime(date);
        codeUrl.setUpdateTime(date);
        LowCodeUrl insert = mongoTemplateLowCode.insert(codeUrl);
        return ReturnResult.build(Result.ADD_SUCCESS).setData(insert.getId());
    }

    /**
     * 新增 event 数据
     *
     * @param eventParams LowCodeUrl 数据
     * @return ReturnResult<Object>
     */
    @Override
    public ReturnResult<Object> addEvent(AddEventParams eventParams) {
        Date date = new Date();
        Event event = new Event();
        event.setEventName(eventParams.getEventName());
        event.setEventType(eventParams.getEventType());
        event.setTriggerMode(eventParams.getTriggerMode());
        event.setCreateTime(date);
        event.setUpdateTime(date);
        Event insert = mongoTemplateEvent.insert(event);
        return ReturnResult.build(Result.ADD_SUCCESS).setData(insert.getId());
    }
}
